package vn.edu.fpt.xml.hrm.controller.approval;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

import vn.edu.fpt.xml.hrm.common.AppConstant;
import vn.edu.fpt.xml.hrm.common.CheckUtil;
import vn.edu.fpt.xml.hrm.common.FormatUtil;
import vn.edu.fpt.xml.hrm.controller.AbstractController;
import vn.edu.fpt.xml.hrm.model.ActivityBean;
import vn.edu.fpt.xml.hrm.model.ProjectBean;
import vn.edu.fpt.xml.hrm.pojo.Activity;
import vn.edu.fpt.xml.hrm.pojo.Project;

public class ListActivitiesController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3251181563123293843L;

	@Override
	protected String execute() throws SQLException, ClassNotFoundException {
		Set<Activity> actList = null;
		Set<Project> proList = null;
		proList = getProList();
		actList = getActList(proList);
		getRequest().setAttribute("proList", proList);
		getRequest().setAttribute("actList", actList);
		return AppConstant.URL_LOG_02010;
	}
	
	private Set<Project> getProList() throws SQLException, ClassNotFoundException {
		Set<Project> proList = null;
		ProjectBean projectBean = null;
//		User user = null;
		//TODO get user id from session
		projectBean = new ProjectBean();
//		user = (User) getRequest().getSession().getAttribute("user");
//		proList = projectBean.getProjectList(user.getUserID());
		proList = projectBean.getProjectList(5);
		return proList;
	}
	
	private Set<Activity> getActList(Set<Project> proList) throws SQLException, ClassNotFoundException {
		Set<Activity> actList = null;
		ActivityBean activityBean = null;
		String fromYear = null;
		String fromMonth = null;
		String fromDay = null;
		Date fromDate = null;
		String toYear = null;
		String toMonth = null;
		String toDay = null;
		Date toDate = null;
		String projectIdS = null;
		String employeeIdS = null;
		int projectId = 0;
		int employeeId = 0;
		String status = null;
		String search = null;
		Calendar calendar = null;
		search = getRequest().getParameter("search");
		if (search != null) {
			// get input parameter
			fromYear = getRequest().getParameter("fromYear");
			fromMonth = getRequest().getParameter("fromMonth");
			fromDay = getRequest().getParameter("fromDay");
			toYear = getRequest().getParameter("toYear");
			toMonth = getRequest().getParameter("toMonth");
			toDay = getRequest().getParameter("toDay");
			projectIdS = getRequest().getParameter("proId");
			employeeIdS = getRequest().getParameter("empId");
			status = getRequest().getParameter("status");
			// process input
			if (!CheckUtil.isBlankOrNull(fromYear)
					&& !CheckUtil.isBlankOrNull(fromMonth)
					&& !CheckUtil.isBlankOrNull(fromDay)) {
				fromDate = FormatUtil
						.stringToDate(fromYear, fromMonth, fromDay);
			}
			if (!CheckUtil.isBlankOrNull(toYear)
					&& !CheckUtil.isBlankOrNull(toMonth)
					&& !CheckUtil.isBlankOrNull(toDay)) {
				toDate = FormatUtil.stringToDate(toYear, toMonth, toDay, "23",
						"59", "59");
			}
			projectId = Integer.parseInt(projectIdS);
			employeeId = Integer.parseInt(employeeIdS);
		} else {
			// setup initial search condition
			calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -14);
			fromDate = calendar.getTime();
		}
		activityBean = new ActivityBean();
		actList = activityBean.getApproveList(fromDate, toDate, projectId, proList, employeeId, status);
		return actList;
	}

}
